const {
  $util,
} = getApp();
Page({
  data: {
    topicList: [],
    max: 100,
  },
  onLoad() {

  },
  onShow() {
    this.loadTopicList()
  },

  // 获取题库
  loadTopicList() {
    $util.request({
      url: 'entry/wxapp/month.report.topiclist',
      data: {},
      success: (res) => {
        console.log(res.list)
        this.setData({
          topicList: res.list,
        })
      },
    });
  },

  // 输入分数
  inputScore(e) {
    // 最大值不超过100
    if (this.data.max < e.detail.value) {
      return 100;
    }

    // 正则表达式，小数点后保留一位
    if (/^(\d?)+(\.\d{0,1})?$/.test(e.detail.value)) {
      return e.detail.value;
    } else {
      return e.detail.value.substring(0, e.detail.value.length - 1);
    }
  },

  // 提交表单
  formSubmit(e) {
    wx.showModal({
      title: '提示',
      content: '是否确认提交？',
      success: (res) => {
        if (res.confirm) {
          this.submit(e)
        } else if (res.cancel) {
          return
        }
      }
    })
  },

  submit(e) {
    let res_str = [];
    for (var i in this.data.topicList) {
      for (var k in this.data.topicList[i]) {
        let plus_grade = 'plus_grade' + this.data.topicList[i][k]['topic_id'];
        let reduce_grade = 'reduce_grade' + this.data.topicList[i][k]['topic_id'];
        let last_grade = e.detail.value[plus_grade] - e.detail.value[reduce_grade];
        let submit_arr = {};
        // 判断是否有题目未写
        if (e.detail.value[plus_grade] == '' || e.detail.value[reduce_grade] == '') {
          wx.showModal({
            title: '提示',
            content: '您第' + this.data.topicList[i][k].classify_displayorder + '题未填写，请填写后提交',
          })
          return;
        }
        submit_arr.plus_grade = e.detail.value[plus_grade];
        submit_arr.reduce_grade = e.detail.value[reduce_grade];
        submit_arr.topic_id = this.data.topicList[i][k]['topic_id'];
        submit_arr.record_id = this.data.topicList[i][k]['record_id'];
        submit_arr.last_grade = last_grade;
        res_str.push(submit_arr);
      }
    }

    res_str = JSON.stringify(res_str);
    $util.request({
      url: 'entry/wxapp/month.report.writegrade',
      data: {
        res_str: res_str
      },
      success: (res) => {
        setTimeout(function(){
          wx.navigateBack({
            delta: 1, // 返回上一级页面。

          })
        },2000)
        
      }
    });
  }
});